package com.cf.log.core.holder;

import com.cf.log.core.LogConstant;
import com.cf.log.core.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * game-server-SpringMvcHolderInterceptor
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/16 下午 01:48
 */
public abstract class SpringMvcHolderInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        User user = getUser(request, response, handler);
        if (user != null) {
            RequestHolder.set(LogConstant.userKey, user);
        }
        return true;
    }

    protected abstract User getUser(HttpServletRequest request, HttpServletResponse response, Object handler);

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        RequestHolder.clean();
        super.afterCompletion(request, response, handler, ex);
    }


}
